package cn.htsyb.synergy.security.web.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration(proxyBeanMethods = false)
@EnableWebSecurity(debug = true)
@EnableMethodSecurity(prePostEnabled = true)
public class OAuthSecurityConfig {
    //用户密码模式登陆
    @Bean
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeHttpRequests(authorizeRequests -> authorizeRequests
                        .requestMatchers("/password/*").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(Customizer.withDefaults())
                .cors()
                .and()
                .sessionManagement()
                //由于前后端分离，所以将服务端的session策略设置为无状态
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .oauth2ResourceServer()
                .jwt();

        return httpSecurity.build();
    }


//    @Bean
//    public BCryptPasswordEncoder passwordEncoder(){
//        return  new BCryptPasswordEncoder();
//    }


    /**
     * 在内存中抽象一个Spring Security安全用户{@link User}，同时该用户也是Resource Owner;
     * 实际开发中需要持久化到数据库。
     *
     * @return the user details service
     */
    @Bean
    public UserDetailsService userDetailsService() {

        UserDetails userDetails = User.builder()
                .username("username")
                .password("{noop}password")//
                .passwordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder()::encode)
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(userDetails);
    }
    /**
     * 开放一些端点的访问控制。
     *
     * 如果你使用了一些依赖这些端点的程序，比如Consul健康检查；
     * 打开H2数据库web控制台访问控制，方便你查看数据具体看配置文件说明。
     *
     * @return the web security customizer
     */
    @Bean
    WebSecurityCustomizer webSecurityCustomizer() {
        return web -> web.ignoring().requestMatchers("/actuator/health","/h2-console/**");
    }
}